package practice1_100;

import java.util.*;

public class Practice1006 {
        public int clumsy(int N) {
            if(N==1) return 1;
            int val=N-1;
            int count=1;
            Stack<Integer> stackVal =new Stack<>();
            Stack<Character> stackChar=new Stack<>();
            stackVal.push(N);
            for (int i = 2; i <=N; i++,val--) {
                if((i-1)%4==1){
                    count=stackVal.pop();
                    stackVal.push(count*val);
                }else if((i-1)%4==2){
                    count=stackVal.pop();
                    stackVal.push(count/val);
                }else if((i-1)%4==3){
                    stackVal.push(val);
                    stackChar.push('+');
                }else {
                    stackVal.push(val);
                    stackChar.push('-');
                }
            }

            Stack<Character> stackChar1=new Stack<>();
            Stack<Integer> stackVal1=new Stack<>();
            while (!stackChar.isEmpty()){
                stackChar1.add(stackChar.pop());
            }
            while (!stackVal.isEmpty()){
                stackVal1.add(stackVal.pop());
            }

            count=stackVal1.pop();
            while (!stackChar1.isEmpty()){
                if(stackChar1.pop()=='-'){
                    count-=stackVal1.pop();
                }else {
                    count+=stackVal1.pop();
                }
            }
            return count;
        }



    public int clumsy1(int N) {
        if (N == 1) return 1;
        int val = N - 1;
        int count = 1;
        Stack<Integer> stackVal = new Stack<>();
        stackVal.push(N);
        for (int i = 2; i <= N; i++, val--) {
            if ((i - 1) % 4 == 1) {
                count = stackVal.pop();
                stackVal.push(count * val);
            } else if ((i - 1) % 4 == 2) {
                count = stackVal.pop();
                stackVal.push(count / val);
            } else if ((i - 1) % 4 == 3) {
                stackVal.push(val);
            } else {
                stackVal.push(-val);
            }
        }
        int res=0;
        while (!stackVal.isEmpty()){
          res+=stackVal.pop();
        }
        return  res;
    }
    public static void main(String[] args) {
       int a= new Practice1006().clumsy(1000);
        System.out.println(a);
    }
}
